package org.dromara.web.controller.business;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.UserConstants;
import org.dromara.common.core.domain.model.LoginBody;
import org.dromara.common.core.utils.MessageUtils;
import org.dromara.common.core.utils.ServletUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.dao.system.domain.vo.SysClientVo;
import org.dromara.web.controller.business.req.UserReq;
import org.dromara.web.domain.user.UserAssetsTotal;
import org.dromara.web.domain.vo.LoginVo;
import org.dromara.web.domain.vo.RemoveRequest;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.web.service.auth.IAuthStrategy;
import org.dromara.web.service.system.ISysClientService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.dao.business.domain.User;
import org.dromara.dao.business.domain.UserQuery;
import org.dromara.web.service.business.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.dromara.common.mybatis.core.page.TableDataInfo;


/**
 * 用户列表 控制器
 *
 * @date 2024-09-14 08:20:08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/user/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 查询用户列表列表
     */
    @SaCheckPermission("user:user:list")
    @GetMapping("/list")
    public TableDataInfo<User> list(UserQuery query, PageQuery pageQuery) {

        return userService.queryPageList(query, pageQuery);
    }




    /**
     * 获取用户列表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("user:user:query")
    @GetMapping("/info")
    public R<User> getInfo(@NotNull(message = "主键不能为空")
                                     @RequestParam Long id) {
        return R.ok(userService.queryById(id));
    }

    /**
     * 新增用户列表
     */
    @SaCheckPermission("user:user}:add")
    @Log(title = "用户列表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add( @RequestBody UserReq.UserAddReq entity) {
        userService.adminRegister(entity);
        return R.ok();
    }

    /**
     * 修改用户列表
     */
    @SaCheckPermission("user:user:edit")
    @Log(title = "用户列表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/update")
    public R<Void> edit( @RequestBody User update) {
        userService.updateById(update);
        return R.ok();
    }

    /**
     * 删除用户列表
     *
     * @param req 主键串
     */
    @SaCheckPermission("user:user:remove")
    @Log(title = "用户列表", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public R<Void> remove(@RequestBody RemoveRequest req) {
        userService.deleteByIds(req.getIds());
        return R.ok();
    }




    @PostMapping("/balanceUpdate")
    public R<Void> balanceUpdate(@RequestBody UserReq.UserBalanceUpdateReq req) {
        if (req.getDiamond() == null) req.setDiamond(0);
        if (req.getStar() == null)  req.setStar(0);
        if (req.getDiamond() != 0 || req.getStar() != 0) {

            userService.userBalanceChange(req.getUserId(), req.getDiamond(), req.getStar(), "系统调整");
        }
        return R.ok();
    }


}
